var table;

$(document).ready(function () {
    table = $('#table_users').DataTable({
        ajax: {
            url: "/Trabalho_PD/webresources/WebServiceBoleias/getAllUsers/",
            type: "GET",
            dataSrc: "users"
        },
        columns: [
            {data: 'name'},
            {data: 'identity_number'},
            {data: 'email'},
            {
                data: function (row, type, val, meta) {
                    if (row.awaiting) {
                        return {type: 0, hello_message: row.hello_message};
                    }
                    if (row.cancelled) {
                        if (row.unlock) {
                            return {type: 1, reason: row.reason, canceling_date: row.canceling_date};
                        }
                    }
                    return {type: 2};
                },
                render: function (data, type, full, meta) {
                    if (data.type === 0) {
                        return '<span class="btn btn-primary details-control1"><i class="fa fa-user-plus fa-2x"></i> <i class="fa fa-exclamation fa-2x"></i></span>';
                    } else if (data.type === 1) {
                        return '<span class="btn btn-primary details-control2"><i class="fa fa-user fa-2x"></i> <i class="fa fa-exclamation fa-2x"></i></span>';
                    } else {
                        return ' ';
                    }
                }
            }
        ]
    });

    $('#table_users tbody').on('click', 'span.details-control1', function () {
        var tr = $(this).closest('tr');
        var row = table.row(tr);

        if (row.child.isShown()) {
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            row.child(createWaitingHtml(row.data(), row.index())).show();
            tr.addClass('shown');
        }
    });

    $('#table_users tbody').on('click', 'span.details-control2', function () {
        var tr = $(this).closest('tr');
        var row = table.row(tr);

        if (row.child.isShown()) {
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            row.child(createCancelledHtml(row.data(), row.index())).show();
            tr.addClass('shown');
        }
    });
});

function createWaitingHtml(d, i) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
            '<tr>' +
            '<td>Welcome message:</td>' +
            '<td>' + d.hello_message + '</td>' +
            '</tr>' +
            '<tr>' +
            '<td><button type="button" onclick="onActivateWaitingAccountClick(\'' + i + '\')" class="btn btn-primary"><i class="fa fa-check fa-2x"></i></button></td>' +
            '</tr>' +
            '</table>';
}

function createCancelledHtml(d, i) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
            '<tr>' +
            '<td>Cancelling reason:</td>' +
            '<td>' + d.reason + '</td>' +
            '</tr>' +
            '<tr>' +
            '<td>Cancelling date:</td>' +
            '<td>' + d.canceling_date + '</td>' +
            '</tr>' +
            '<tr>' +
            '<td><button type="button" onclick="onActivateCancelledClick(\'' + i + '\')" class="btn btn-primary"><i class="fa fa-unlock fa-2x"></i></button></td>' +
            '</tr>' +
            '</table>';
}

function onActivateCancelledClick(i) {
    var id_user = table.row(i).data().id_user;
    sendActivateCancelledAccount(id_user, function (data) {
        if (data["success"]) {
            getUser(id_user, function (newUser) {
                var tr = $(this).closest('tr');
                var row = table.row(i);
                row.child.hide();
                tr.removeClass('shown');
                row.data(newUser).draw();
            });
        }
    });
}

function onActivateWaitingAccountClick(i) {
    var id_user = table.row(i).data().id_user;
    sendActivateWaitingAccount(id_user, function (data) {
        if (data["success"]) {
            getUser(id_user, function (newUser) {
                var tr = $(this).closest('tr');
                var row = table.row(i);
                row.child.hide();
                tr.removeClass('shown');
                row.data(newUser).draw();
            });
        }
    });
}
